﻿using Learun.Application.Organization;
using Learun.Util.SqlSugar;
using System.Collections.Generic;
using System.Linq;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：自动化平台服务层
    /// </summary>
    public class IO_WorkFlowService : ec_Wire_GroupService
    {
        public enum Department
        {
            轮机 = 0,
            电气 = 1
        }

        ec_projectEntity _Project;
        public string ProjIndex = "";
        UserEntity _User;
        CompanyEntity _Company;
        /// <summary>
        /// 当前用户所在的部门
        /// </summary>
        public Department _Department;
        string SignalTableName = "";
        public string NoticeTableName = "";
        string LogTableName = "";
        #region 仓储
        public Repository<ec_wire_group_noticeEntity> _noticeRepository => new Repository<ec_wire_group_noticeEntity>();
        #endregion
        public IO_WorkFlowService(string projId, string userId)
        {
            _Project = SqlSugarHelper.Db.Queryable<ec_projectEntity>().Single(x => x.ProjectId == projId);
            if (_Project != null)
            {
                ProjIndex = _Project.ProjectIndex.ToString();
                SignalTableName = ProjectSugar.TableName<ec_Wire_GroupEntity>(ProjIndex);
                NoticeTableName = ProjectSugar.TableName<ec_wire_group_noticeEntity>(ProjIndex);
                LogTableName = ProjectSugar.TableName<ec_wire_group_logEntity>(ProjIndex);
            }
            _User = SqlSugarHelper.Db.Queryable<UserEntity>().Single(x => x.F_UserId == userId);
            _Company = SqlSugarHelper.Db.Queryable<CompanyEntity>().First(x => x.F_CompanyId == _User.F_CompanyId);
            var DepartmentStr = "";
            if (_Company != null)
            {
                DepartmentStr = _Company.F_FullName;
                if (DepartmentStr.Contains("轮机"))
                {
                    _Department = Department.轮机;
                }
                else
                {
                    _Department = Department.电气;
                }
            }
            else
            {
                _Department = Department.电气;
            }
        }
        /// <summary>
        /// 没用，就是单纯为了方便复制if语句
        /// </summary>
        void test(ec_Wire_GroupEntity NewSignal)
        {
            if (_Department == Department.电气 && NewSignal.ElecOnly)
            {
                #region E-E  
                #endregion
            }
            else if (_Department == Department.电气 && !NewSignal.ElecOnly)
            {
                #region E-M
                #endregion
            }
            else if (_Department == Department.轮机 && NewSignal.ElecOnly)
            {
                #region M-E
                #endregion
            }
            else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
            {
                #region M-M
                #endregion
            }
        }
        //新增 = 0, 
        //修改 = 1,
        //准备删除 = 2,
        //撤销删除 = 3,
        //删除 = 4,
        //关联通道 = 5,
        //审核 = 6,
        //重新打开=7,
        #region New
        /// <summary>
        /// 新增信号
        /// </summary>
        /// <param name="NewSignal"></param>
        /// <param name="NewId"></param>
        /// <param name="NeedLog"></param>
        /// <param name="OnlyMessage"></param>
        /// <returns></returns>
        public string Add(ec_Wire_GroupEntity NewSignal, ref string NewId, bool NeedLog = true, bool OnlyMessage = false)
        {
            if (string.IsNullOrEmpty(NewSignal.Group_Name))
            {
                NewSignal.Group_Name = NewSignal.Signal_Group + NewSignal.Signal_SeqNo;
            }
            var existObj = _wireGroupRepository.AsQueryable().AS(SignalTableName).First(x => x.Group_Name.ToUpper() == NewSignal.Group_Name.ToUpper());
            if (existObj != null)
            {
                if (existObj.DeleteFlg)
                {
                    return $"{existObj.Group_Name}无法新建，该通道号曾经被使用，虽然已经被删除，但是无法再使用了。";
                }
                else
                {
                    return $"{existObj.Group_Name}无法新建，已有一个同名的信号存在。";
                }

            }
            if (!OnlyMessage)
            {
                NewSignal.Create();
                _wireGroupRepository.AsInsertable(NewSignal).AS(SignalTableName).ExecuteCommand();
                NewId = NewSignal.Wire_Group_ID;

                #region 通知
                if (_Department == Department.电气 && NewSignal.ElecOnly)
                {
                    #region E新建E  
                    #endregion
                }
                else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                {
                    #region E新建M
                    var noticeObj = new ec_wire_group_noticeEntity(Action.新增, Department.轮机, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                {
                    #region M新建E
                    if (_wireGroupRepository.GetList().Count() > 0)//第一次空白批量导入时，不需要。即公司级里有没有东西
                    {
                        var noticeObj = new ec_wire_group_noticeEntity(Action.新增, Department.电气, NewSignal.Wire_Group_ID);
                        noticeObj.Create();
                        _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    }
                    #endregion
                }
                else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
                {
                    #region M新建M
                    if (_wireGroupRepository.GetList().Count() > 0)//第一次空白批量导入时，不需要
                    {
                        var noticeObj = new ec_wire_group_noticeEntity(Action.新增, Department.电气, NewSignal.Wire_Group_ID);
                        noticeObj.Create();
                        _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    }

                    #endregion
                }
                #endregion
            }

            #region 日志
            if (NeedLog)
            {
                var allLogs = new List<ec_wire_group_logEntity>();
                var log = new ec_wire_group_logEntity(Action.新增, NewSignal.Wire_Group_ID);
                allLogs.Add(log);
                SqlSugarHelper.Db.Insertable(allLogs).AS(LogTableName).ExecuteCommand();
            }
            #endregion
            return "";
        }

        #endregion
        #region Modify
        public string Modify(ec_Wire_GroupEntity NewSignal, ec_Wire_GroupEntity existObj, string MODE = "0", bool OnlyMessage = false)
        {

            if (MODE != "0")
            {
                //从IO管理界面进来
                this.SaveEntity(_Project.ProjectId, NewSignal.Wire_Group_ID, NewSignal, MODE);
            }
            else
            {
                if (NewSignal.Status == WireGroupStatusEnum.Confirmed)
                {
                    if (OnlyMessage)
                    {
                        return $"{NewSignal.Group_Name}待删除信号不能审核。";
                    }
                    return $"{NewSignal.Group_Name}无法编辑，因为已经被审核，需要重新打开后才能编辑。";
                }
                //var existObj = this.GetEntity(ProjIndex, NewSignal.Wire_Group_ID);
                if (existObj != null && !OnlyMessage)
                {
                    string id = existObj.Wire_Group_ID;


                    var ModifyMsg = this.CompareModifyField(ProjIndex, existObj, NewSignal);//信号修改
                    #region 通知
                    if (!string.IsNullOrEmpty(ModifyMsg))
                    {
                        if (string.IsNullOrEmpty(existObj.ChannelID))
                        {
                            #region 关联前

                            if (_Department == Department.电气 && NewSignal.ElecOnly)
                            {
                                #region E-E 

                                #endregion
                            }
                            else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                            {
                                #region E-M
                                var noticeObj = new ec_wire_group_noticeEntity(Action.修改, Department.轮机, NewSignal.Wire_Group_ID);
                                noticeObj.Create();
                                noticeObj.Message = ModifyMsg;//属性的一些修改记录
                                _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                                #endregion
                            }
                            else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                            {
                                #region M-E

                                #endregion
                            }
                            else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
                            {
                                #region M-M

                                #endregion
                            }
                            #endregion

                            if (!string.IsNullOrEmpty(NewSignal.ChannelID))
                            {
                                #region 关联正在进行
                                var log = new ec_wire_group_logEntity(Action.关联通道, NewSignal.Wire_Group_ID);
                                SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
                                NewSignal.Status = WireGroupStatusEnum.Used;
                                #endregion
                            }
                        }
                        else
                        {
                            #region 关联后

                            if (_Department == Department.电气 && NewSignal.ElecOnly)
                            {
                                #region E-E  
                                #endregion
                            }
                            else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                            {
                                #region E-M
                                var noticeObj = new ec_wire_group_noticeEntity(Action.修改, Department.轮机, NewSignal.Wire_Group_ID);
                                noticeObj.Create();
                                noticeObj.Message = ModifyMsg;//属性的一些修改记录
                                _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                                #endregion
                            }
                            else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                            {
                                #region M-E
                                var noticeObj = new ec_wire_group_noticeEntity(Action.修改, Department.电气, NewSignal.Wire_Group_ID);
                                noticeObj.Create();
                                noticeObj.Message = ModifyMsg;//属性的一些修改记录
                                _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                                #endregion
                            }
                            else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
                            {
                                #region M-M
                                var noticeObj = new ec_wire_group_noticeEntity(Action.修改, Department.电气, NewSignal.Wire_Group_ID);
                                noticeObj.Create();
                                noticeObj.Message = ModifyMsg;//属性的一些修改记录
                                _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                                #endregion
                            }
                            #endregion
                            if (string.IsNullOrEmpty(NewSignal.ChannelID))
                            {
                                #region 关联解除
                                var log = new ec_wire_group_logEntity(Action.关联解除, NewSignal.Wire_Group_ID);
                                SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
                                NewSignal.Status = WireGroupStatusEnum.New;
                                #endregion
                            }

                        }
                    }
                    #endregion
                    this.SaveEntity(_Project.ProjectId, NewSignal.Wire_Group_ID, NewSignal, MODE);
                    //NewSignal.Modify(id);
                    //_wireGroupRepository.AsUpdateable(NewSignal).AS(SignalTableName).
                    //    IgnoreColumns(x => x.CreateTime).                            
                    //    IgnoreColumns(x => x.CreateUserID).ExecuteCommand();
                }
            }

            return "";
        }
        #endregion
        #region 准备删除
        public string ReadyDelete(ec_Wire_GroupEntity NewSignal, ec_Wire_GroupEntity OldSignal, string DeleteReason, bool NeedLog = true, string MODE = "0", bool OnlyMessage = false)
        {
            string id = NewSignal.Wire_Group_ID;
            string res = "";
            if (string.IsNullOrEmpty(DeleteReason))
            {
                res = "删除时，需要有一个说明原因";
            }
            if (!string.IsNullOrEmpty(res))
            {
                return res;
            }
            if (!OnlyMessage)
            {
                if (string.IsNullOrEmpty(OldSignal.ChannelID))
                {
                    #region 关联前
                    if (_Department == Department.电气 && NewSignal.ElecOnly)
                    {
                        #region E-E 
                        //var log = new ec_wire_group_logEntity(Action.准备删除, NewSignal.Wire_Group_ID);
                        //log.ExtraMsg = DeleteReason;
                        //SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
                        #endregion
                    }
                    else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                    {
                        #region E-M
                        var noticeObj = new ec_wire_group_noticeEntity(Action.准备删除, Department.轮机, NewSignal.Wire_Group_ID);
                        noticeObj.Create();
                        noticeObj.Message = DeleteReason;//删除原因
                        _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                        #endregion
                    }
                    else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                    {
                        #region M-E
                        //var log = new ec_wire_group_logEntity(Action.准备删除, NewSignal.Wire_Group_ID);
                        //log.ExtraMsg = DeleteReason;
                        //SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
                        #endregion
                    }
                    else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
                    {
                        #region M-M
                        //var log = new ec_wire_group_logEntity(Action.准备删除, NewSignal.Wire_Group_ID);
                        //log.ExtraMsg = DeleteReason;
                        //SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
                        #endregion
                    }
                    #endregion
                }
                else
                {
                    #region 关联后

                    if (_Department == Department.电气 && NewSignal.ElecOnly)
                    {
                        #region E-E  
                        //var log = new ec_wire_group_logEntity(Action.准备删除, NewSignal.Wire_Group_ID);
                        //log.ExtraMsg = DeleteReason;
                        //SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
                        #endregion
                    }
                    else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                    {
                        #region E-M
                        var noticeObj = new ec_wire_group_noticeEntity(Action.准备删除, Department.轮机, NewSignal.Wire_Group_ID);
                        noticeObj.Create();
                        noticeObj.Message = DeleteReason;  //属性的一些修改记录
                        _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                        #endregion
                    }
                    else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                    {
                        #region M-E
                        var noticeObj = new ec_wire_group_noticeEntity(Action.准备删除, Department.电气, NewSignal.Wire_Group_ID);
                        noticeObj.Create();
                        noticeObj.Message = DeleteReason;//属性的一些修改记录
                        _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                        #endregion
                    }
                    else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
                    {
                        #region M-M
                        var noticeObj = new ec_wire_group_noticeEntity(Action.准备删除, Department.电气, NewSignal.Wire_Group_ID);
                        noticeObj.Create();
                        noticeObj.Message = DeleteReason;//属性的一些修改记录
                        _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                        #endregion
                    }
                    #endregion
                }

                this.SaveEntity(ProjIndex, NewSignal.Wire_Group_ID, NewSignal, MODE);
            }
            #region log
            if (NeedLog)
            {
                var log = new ec_wire_group_logEntity(Action.准备删除, NewSignal.Wire_Group_ID);
                log.ExtraMsg = DeleteReason;
                SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
            }
            #endregion
            return "";
        }
        #endregion
        #region 彻底删除
        /// <summary>
        /// 只能删除对方部门都确认过的点（即这个信号还需要对方专业确认时，是没法删的）
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="Wire_Group_ID"></param>
        /// <param name="OnlyMessage">只返回是否可删除</param>
        /// <returns>""，说明可以删。否则说明不能删（比如有未读的notice）</returns>
        public string Delete(string ProjectId, string Wire_Group_ID, bool OnlyMessage)
        {
            var existObj = this.GetEntity(ProjectId, Wire_Group_ID);
            if (existObj != null)
            {
                if (existObj.Status == WireGroupStatusEnum.ToDelete)
                {
                    if (!IsToDoList(Wire_Group_ID, _Department == Department.电气 ? Department.轮机 : Department.电气))
                    {
                        if (string.IsNullOrEmpty(existObj.ChannelID))
                        {
                            #region 关联前
                            if (_Department == Department.电气 && existObj.ElecOnly && !OnlyMessage)
                            {
                                #region E-E 
                                //_wireGroupRepository.AsDeleteable().AS(SignalTableName).Where(x => x.Wire_Group_ID == Wire_Group_ID).ExecuteCommand();
                                this.DeleteEntity(ProjectId, Wire_Group_ID);
                                #endregion
                            }
                            else if (_Department == Department.电气 && !existObj.ElecOnly)
                            {
                                #region E-M
                                return $"{existObj.Group_Name}无法删除，该信号属于【轮机】确认的点，只能由轮机专业删除。";
                                #endregion
                            }
                            else if (_Department == Department.轮机 && existObj.ElecOnly)
                            {
                                #region M-E
                                return $"{existObj.Group_Name}无法删除，该信号属于【电气】确认的点，只能由电气专业删除。";
                                #endregion
                            }
                            else if (_Department == Department.轮机 && !existObj.ElecOnly && !OnlyMessage)
                            {
                                #region M-M 
                                //_wireGroupRepository.AsDeleteable().AS(SignalTableName).Where(x => x.Wire_Group_ID == Wire_Group_ID).ExecuteCommand();
                                this.DeleteEntity(ProjectId, Wire_Group_ID);
                                #endregion
                            }
                            #endregion
                        }
                        else
                        {
                            #region 关联后

                            if (_Department == Department.电气 && existObj.ElecOnly && !OnlyMessage)
                            {
                                #region E-E   
                                //_wireGroupRepository.AsDeleteable().AS(SignalTableName).Where(x => x.Wire_Group_ID == Wire_Group_ID).ExecuteCommand();
                                this.DeleteEntity(ProjectId, Wire_Group_ID);
                                #endregion
                            }
                            else if (_Department == Department.电气 && !existObj.ElecOnly)
                            {
                                #region E-M
                                return $"{existObj.Group_Name}无法删除，该信号属于【轮机】确认的点，只能由轮机专业删除。";
                                #endregion
                            }
                            else if (_Department == Department.轮机 && existObj.ElecOnly)
                            {
                                #region M-E
                                return $"{existObj.Group_Name}无法删除，该信号属于【电气】确认的点，只能由电气专业删除。";
                                #endregion
                            }
                            else if (_Department == Department.轮机 && !existObj.ElecOnly && !OnlyMessage)
                            {
                                #region M-M 
                                //_wireGroupRepository.AsDeleteable().AS(SignalTableName).Where(x => x.Wire_Group_ID == Wire_Group_ID).ExecuteCommand();
                                this.DeleteEntity(ProjectId, Wire_Group_ID);
                                #endregion
                            }
                            #endregion
                        }
                        return "";
                    }
                    else
                    {
                        //仍有未确认的notice对象。
                        //不能删
                        return $"{existObj.Group_Name}无法删除，该信号还有待确认的未读消息在对方专业，请先确认。";

                    }
                }
                else
                {
                    return $"{existObj.Group_Name}无法删除，只有“待删除”状态的信号才能执行删除。";
                }
            }
            else
            {
                return $"{existObj.Group_Name}无法删除，数据库中找不到该信号。";
            }
        }
        #endregion
        #region 关联通道
        //包含在修改里
        #endregion
        #region 审核
        /// <summary>
        /// 只能审批自己部门的信号，并且关联过、没有待处理流程
        /// </summary>
        /// <param name="NewSignal"></param>
        public string Approve(ec_Wire_GroupEntity NewSignal, bool NeedLog = true, string MODE = "0", bool OnlyMessage = false)
        {
            if (!IsToDoList(NewSignal.Wire_Group_ID, _Department == Department.电气 ? Department.轮机 : Department.电气))
            {
                string id = NewSignal.Wire_Group_ID;
                //if (!string.IsNullOrEmpty(NewSignal.ChannelID))
                //{
                if (NeedLog)
                {
                    var log = new ec_wire_group_logEntity(Action.审核, NewSignal.Wire_Group_ID);
                    SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
                }

                #region 关联后
                if (_Department == Department.电气 && NewSignal.ElecOnly && !OnlyMessage)
                {
                    #region E-E
                    var noticeObj = new ec_wire_group_noticeEntity(Action.审核, Department.轮机, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                {
                    #region E-M
                    return $"{NewSignal.Group_Name}无法审核，该信号属于【轮机】确认的点，只能由轮机专业审核。若修改专业，请先保存！";
                    #endregion
                }
                else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                {
                    #region M-E
                    return $"{NewSignal.Group_Name}无法审核，该信号属于【电气】确认的点，只能由电气专业审核。若修改专业，请先保存！";
                    #endregion
                }
                else if (_Department == Department.轮机 && !NewSignal.ElecOnly && !OnlyMessage)
                {
                    #region M-M
                    var noticeObj = new ec_wire_group_noticeEntity(Action.审核, Department.电气, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                #endregion
                if (!OnlyMessage)
                {
                    this.SaveEntity(ProjIndex, NewSignal.Wire_Group_ID, NewSignal, MODE);
                }
                return "";
                //}
                //else
                //{
                //    return $"{NewSignal.Group_Name}无法审核通过，只有“已关联”状态的信号才能执行审核。";
                //}
            }
            else
            {
                return $"{NewSignal.Group_Name}无法审核通过，该信号还有待确认的未读消息在对方专业，请先确认。";

            }
        }
        #endregion
        #region 重新打开
        /// <summary>
        /// 打开并又被编辑过的情况，才需要发通知
        /// </summary>
        /// <param name="NewSignal"></param>
        public void Reopen(ec_Wire_GroupEntity NewSignal, ec_Wire_GroupEntity OldSignal, string reason, bool NeedLog = true, string MODE = "0")
        {
            string id = NewSignal.Wire_Group_ID;
            var ModifyMsg = this.CompareModifyField(ProjIndex, OldSignal, NewSignal, "Status");//信号重新打开

            if (!string.IsNullOrEmpty(OldSignal.ChannelID) && !string.IsNullOrEmpty(ModifyMsg))
            {
                #region 关联后

                if (_Department == Department.电气 && NewSignal.ElecOnly)
                {
                    #region E-E
                    #endregion
                }
                else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                {
                    #region E-M
                    var noticeObj = new ec_wire_group_noticeEntity(Action.重新打开, Department.轮机, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    noticeObj.Message = reason + "\n" + ModifyMsg;//属性的一些修改记录
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                {
                    #region M-E
                    var noticeObj = new ec_wire_group_noticeEntity(Action.重新打开, Department.电气, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    noticeObj.Message = reason + "\n" + ModifyMsg;//属性的一些修改记录
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
                {
                    #region M-M
                    var noticeObj = new ec_wire_group_noticeEntity(Action.重新打开, Department.电气, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    noticeObj.Message = reason + "\n" + ModifyMsg;//属性的一些修改记录
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                #endregion
            }

            if (NeedLog)
            {
                var log = new ec_wire_group_logEntity(Action.重新打开, NewSignal.Wire_Group_ID);
                log.ExtraMsg = reason;
                SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
            }
            this.SaveEntity(ProjIndex, NewSignal.Wire_Group_ID, NewSignal, MODE);
        }
        #endregion
        #region 撤销删除(恢复)
        /// <summary>
        /// 可能变为新建、可能变为已关联
        /// </summary>
        /// <param name="NewSignal"></param>
        public void UnDoDelete(ec_Wire_GroupEntity NewSignal, bool NeedLog = true, string MODE = "0")
        {
            string id = NewSignal.Wire_Group_ID;
            #region 通知
            if (_Department == Department.电气 && NewSignal.ElecOnly)
            {
                #region E-E
                #endregion
            }
            else if (_Department == Department.电气 && !NewSignal.ElecOnly)
            {
                #region E-M
                var noticeObj = new ec_wire_group_noticeEntity(Action.撤销删除, Department.轮机, NewSignal.Wire_Group_ID);
                noticeObj.Create();
                _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                #endregion
            }
            else if (_Department == Department.轮机 && NewSignal.ElecOnly)
            {
                #region M-E
                var noticeObj = new ec_wire_group_noticeEntity(Action.撤销删除, Department.电气, NewSignal.Wire_Group_ID);
                noticeObj.Create();
                _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                #endregion
            }
            else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
            {
                #region M-M
                var noticeObj = new ec_wire_group_noticeEntity(Action.撤销删除, Department.电气, NewSignal.Wire_Group_ID);
                noticeObj.Create();
                _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                #endregion
            }
            #endregion

            #region log
            if (NeedLog)
            {
                var log = new ec_wire_group_logEntity(Action.撤销删除, NewSignal.Wire_Group_ID);
                SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
            }
            #endregion
            if (string.IsNullOrEmpty(NewSignal.ChannelID))
            {
                NewSignal.Status = WireGroupStatusEnum.New;
            }
            else
            {
                NewSignal.Status = WireGroupStatusEnum.Used;
            }
            this.SaveEntity(ProjIndex, NewSignal.Wire_Group_ID, NewSignal, MODE);
        }
        #endregion
        #region 迁移
        public string MoveToAnotherCh(ec_Wire_GroupEntity NewSignal, ec_Wire_GroupEntity OldSignal   )
        {
            #region 通知
            //不需要
            #endregion

            #region log

            #endregion
            if (string.IsNullOrEmpty(NewSignal.ChannelID))
            {
                return "";//无效
            }
            else
            { 
                var ModifyMsg = this.CompareModifyField(ProjIndex, OldSignal, NewSignal);//信号修改
                this.SaveEntity(ProjIndex, NewSignal.Wire_Group_ID, NewSignal, "1");//因为model为1时，只会更新ch
                return "";
            }

        }
        #endregion
        #region 从回收站恢复
        /// <summary>
        /// 可能变为新建、可能变为已关联
        /// </summary>
        /// <param name="NewSignal"></param>
        public void Restore(ec_Wire_GroupEntity NewSignal, ec_Wire_GroupEntity OldSignal, string reason, bool NeedLog = true, string MODE = "0")
        {
            string id = NewSignal.Wire_Group_ID;
            var ModifyMsg = this.CompareModifyField(ProjIndex, OldSignal, NewSignal, "Status");//信号从回收站恢复

            if (!string.IsNullOrEmpty(OldSignal.ChannelID) && !string.IsNullOrEmpty(ModifyMsg))
            {
                #region 关联后

                if (_Department == Department.电气 && NewSignal.ElecOnly)
                {
                    #region E-E
                    #endregion
                }
                else if (_Department == Department.电气 && !NewSignal.ElecOnly)
                {
                    #region E-M
                    var noticeObj = new ec_wire_group_noticeEntity(Action.回收站恢复, Department.轮机, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    noticeObj.Message = reason + "\n" + ModifyMsg;//属性的一些修改记录
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                else if (_Department == Department.轮机 && NewSignal.ElecOnly)
                {
                    #region M-E
                    var noticeObj = new ec_wire_group_noticeEntity(Action.回收站恢复, Department.电气, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    noticeObj.Message = reason + "\n" + ModifyMsg;//属性的一些修改记录
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                else if (_Department == Department.轮机 && !NewSignal.ElecOnly)
                {
                    #region M-M
                    var noticeObj = new ec_wire_group_noticeEntity(Action.回收站恢复, Department.电气, NewSignal.Wire_Group_ID);
                    noticeObj.Create();
                    noticeObj.Message = reason + "\n" + ModifyMsg;//属性的一些修改记录
                    _noticeRepository.AsInsertable(noticeObj).AS(NoticeTableName).ExecuteCommand();
                    #endregion
                }
                #endregion
            }

            if (NeedLog)
            {
                var log = new ec_wire_group_logEntity(Action.回收站恢复, NewSignal.Wire_Group_ID);
                log.ExtraMsg = reason;
                SqlSugarHelper.Db.Insertable(log).AS(LogTableName).ExecuteCommand();
            }
            NewSignal.DeleteFlg = false;
            this.SaveEntity(ProjIndex, NewSignal.Wire_Group_ID, NewSignal, MODE);
        }
        #endregion
        /// <summary>
        /// 信号是否在通知中，还有未阅读的记录
        /// </summary>
        /// <param name="wireGroupId"></param>
        /// <param name="department">作为消息确认方的专业</param>
        /// <returns></returns>
        public bool IsToDoList(string wireGroupId, Department department)
        {
            return _noticeRepository.AsQueryable().AS(NoticeTableName).Any(x => x.WireGroupID == wireGroupId && !x.CheckFLG && x.CompanyID == department);
        }
    }
}
